<?php

/*
 * 	the controller handles all the http request on exam report:
 *    1. list all exam items -- default action
 *    2. search indicator by name
 *    3. get indicator specification
 */

class ExamReportController extends Controller {
    /* default action, just call the ListAllItem action */

    public function actionIndex() {
        $this->actionListallItem();
    }

    /*
     * list all the exam items
     * @return json string of the status and info which tells the data or error code.
     */

    //todo: add try-catch 
    public function actionListItem() {
        // var_dump("nihao");exit();
        $type = isset($_GET['type']) ? $_GET['type'] : "*";
        //call model to do the busness logic
        //retrieve all item and conver to array

        try {
            $model = ExamItem::model();
            $all_exam_items = $model->listAll($type);
            $itemsArray = array();
            for ($i = 0; $i < count($all_exam_items); $i++) {
                $itemsArray[$i] = $model->extent2Indicator($all_exam_items[$i]);
            }
            JSONHelper::echoJSON(true, $itemsArray);
        } catch (Exception $exc) {
            JSONHelper::echoJSON(false, JSONHelper::ERROR_SERVICE);
        }
    }

    /*
     * 	search action, url like app/examReport/search&kw=***
     * 	parameter: $kw
     * 	@return indicator name list with its item name 
     */

    public function actionSearchIndicator() {
        $status = Constants::STATUS_OK;
        $info = "";
        if (isset($_GET['kw'])) {
            $keyword = $_GET['kw'];
            $indicatorModel = Indicator::model();
            $data = $indicatorModel->searchByKeyword($keyword);
            $info = $data;
        } else {
            JSONHelper::echoJSON(false,  JSONHelper::ERROR_ILLEGAL_REQUEST);
        }
        //组装成message格式
//        $rs = AppInterfaceHelper::createArrayResult($status, $info);
        JSONHelper::echoJSON(true,$info);
    }
    public function actionSearchIndicatorOnly() {
        $status = Constants::STATUS_OK;
        $info = "";
        if (isset($_GET['kw'])) {
            $keyword = $_GET['kw'];
            $indicatorModel = Indicator::model();
            $data = $indicatorModel->searchOnlyIndicatorByKeyword($keyword);
            $info = $data;
        } else {
            JSONHelper::echoJSON(false,  JSONHelper::ERROR_ILLEGAL_REQUEST);
        }
        //组装成message格式
        JSONHelper::echoJSON(true,$info);
    }

    /*
     * 	retrieve the specification of the indicator given its id as $iid
     * 	@parameter: $iid, the id of indicator
     * 	@return the specification info of the indicator
     */

    public function actionIndicatorSpec($iid) {
        $indicatorSpecArray = ItemIndicator::model()->getIndicatorSpec($iid);
        $ret = AppInterfaceHelper::createArrayResult(Constants::STATUS_OK, $indicatorSpecArray);
        $jsonStr = json_encode($ret);
        echo $jsonStr;
        Yii::app()->end();
    }

    /**
     * 体检项目详情
     */
    public function actionViewMobile() {
        // set layout first:
        $this->layout = '//layouts/mobile';
        // var_dump("expression");exit();
        if(!isset($_GET["id"])){
            throw new CHttpException(200,"",JSONHelper::ERROR_ILLEGAL_REQUEST);
        }
        $id = $_GET["id"];
        // var_dump($id);exit();
        $model = Indicator::model()->findByPk($id);
        // var_dump($model);exit();
        if($model === null){
            throw new CHttpException(200,"",JSONHelper::ERROR_ILLEGAL_REQUEST);
        }

        $value_range = $model->value_ranges;
        // var_dump($value_range);exit();
        if (preg_match('/^((\d+(\.\d*)?),\s*){3}(\d+(\.\d*)?)(\s*\n.*)?$/', $value_range)) {
            $view = 'viewMobile-sexual';
        } elseif (preg_match('/^(?P<min>\d+(\.\d*)?),(?P<max>\d+(\.\d*)?)(\s*\n\s*(?P<unit>\S*)\s*)?$/', $value_range)) {
            $view = 'viewMobile-range';
        } else {
            $view = 'viewMobile';
        }
        // var_dump($view);exit();
        // 2013-10-15, 80x86, remove style tags
        $attrs = $model->attributeLabels();
        foreach ($attrs as $key => $value) {
            $model->$key = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $model->$key);
        }
        // var_dump($model);exit();
        // var_dump($model->over_view);exit();
        $this->render($view, array(
            'model' => $model,
        ));
    }



}
